package com.jsdzt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author ：LiuShihao
 * @date ：Created in 2021/12/16 9:23 上午
 * @desc ：主窗体
 */
public class Ys1901lixintong111 extends JDesktopPane implements ActionListener{

    public static void main(String args[]) {
        try {
            System.out.println("打字通正在运行中...");
            Ys1901lixintong111 mainframe = new Ys1901lixintong111();
            mainframe.setVisible(true);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    Dimension screen;
    Toolkit toolkit=Toolkit.getDefaultToolkit();
    private JMenuBar fMenu;
    private JMenu fMenuSys;//系统
    private JMenu fMenuLx;//练习
    private JMenuItem fMenuKey;//键盘练习
    private JMenuItem fMenuExit;//退出
    private JMenuItem fMenuLoad;//导入文本
    private JMenuItem fMenuRefresh;//刷新文本
    private JMenuItem fMenuCoverkb;//隐藏提示键盘
    private JMenuItem fMenuOpenkb;//打开提示键盘

    //住窗体有两个子窗体，作为主窗体的属性调用时更加方便
    JInternalFrame jianpan;
    Editor edit;
    boolean isInit=false;

    JFrame frame;
    public Ys1901lixintong111() throws IOException {
        JFrame.setDefaultLookAndFeelDecorated(true);//加上一个边框
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置点击关闭退出程序
        Container container = frame.getContentPane();
        container.add(this, BorderLayout.CENTER); //居中排布，JDesktopPane 放在JFrame上
        this.setPreferredSize(new java.awt.Dimension(657, 550));
        frame.setTitle("超级疯狂打字通 你准备好了吗！姐妹！let's go！");//设置标题

        Image ico=Toolkit.getDefaultToolkit().getImage("7.png");
        //设置窗体上的图片，image和icon的方法不同
        frame.setIconImage(ico);
        //设置窗体不可以被手动修改大小
        frame.setResizable(true);
        //设置窗体显示位置
        screen=toolkit.getScreenSize();
        //窗口定位
        frame.setLocation((screen.width-800)/2,screen.height/2-600/2);
        //添加菜单
        fMenu = new JMenuBar();
        frame.setJMenuBar(fMenu);
        fMenu.setPreferredSize(new java.awt.Dimension(392, 23));{
            fMenuSys = new JMenu();
            fMenu.add(fMenuSys);
            fMenuSys.setText("系统(S)");
            fMenuSys.setFont(new Font("x",Font.PLAIN,13));
            //键盘助记符alt组合时激活
            fMenuSys.setMnemonic(java.awt.event.KeyEvent.VK_S);
            //点击后颜色加深，反转效果
            fMenuSys.setRolloverEnabled(true);{
                //1.退出按钮
                fMenuExit = new JMenuItem();
                fMenuSys.add(fMenuExit);
                fMenuExit.setText("退出");
                fMenuExit.addActionListener(this);
                fMenuExit.setFont(new Font("x",Font.PLAIN,12));
                //放置标志
                ImageIcon imageIcon = new ImageIcon(getClass().getClassLoader().getResource("1.png"));
                fMenuExit.setIcon(imageIcon);

                //2.导入文本按钮
                fMenuLoad = new JMenuItem();
                fMenuSys.add(fMenuLoad);
                fMenuLoad.setText("导入文本");
                //导入文本按添加监听器
                fMenuLoad.addActionListener(this);
                fMenuLoad.setFont(new Font("x",Font.PLAIN,12));
                //放置标志
                fMenuLoad.setIcon(new ImageIcon(getClass().getClassLoader().getResource("2.png")));

                //3.隐藏键盘按钮
                fMenuCoverkb = new JMenuItem();
                fMenuSys.add(fMenuCoverkb);
                fMenuCoverkb.setText("隐藏提示键盘");
                //按添加监听器
                fMenuCoverkb.addActionListener(this);
                fMenuCoverkb.setFont(new Font("x",Font.PLAIN,12));
                fMenuCoverkb.setIcon(new ImageIcon(getClass().getClassLoader().getResource("3.png")));

                //4.打开键盘按钮
                fMenuOpenkb = new JMenuItem();
                fMenuSys.add(fMenuOpenkb);
                fMenuOpenkb.setText("打开提示键盘");
                //按添加监听器
                fMenuOpenkb.addActionListener(this);
                fMenuOpenkb.setFont(new Font("x",Font.PLAIN,12));
                fMenuOpenkb.setIcon(new ImageIcon(getClass().getClassLoader().getResource("4.png")));

            }
        }
        {
            //练习菜单按钮
            fMenuLx = new JMenu();
            fMenu.add(fMenuLx);
            fMenuLx.setText("练习(E)");
            fMenuLx.setFont(new Font("x",Font.PLAIN,12));
            fMenuLx.setMnemonic(java.awt.event.KeyEvent.VK_E);
            fMenuLx.setRolloverEnabled(true);
            {
                // 1.键盘练习按钮
                fMenuKey = new JMenuItem();
                fMenuLx.add(fMenuKey);
                fMenuKey.setText("键盘练习");
                //键盘练习按钮添加监听器
                fMenuKey.addActionListener(this);
                fMenuKey.setFont(new Font("x",Font.PLAIN,12));
                fMenuKey.setIcon(new ImageIcon(getClass().getClassLoader().getResource("5.png")));

                //2.更换文本按钮
                fMenuRefresh = new JMenuItem();
                fMenuLx.add(fMenuRefresh);
                fMenuRefresh.setText("更换文本");
                //键盘练习按钮添加监听器
                fMenuRefresh.addActionListener(this);
                fMenuRefresh.setFont(new Font("x",Font.PLAIN,12));
                fMenuRefresh.setIcon(new ImageIcon(getClass().getClassLoader().getResource("6.png")));
            }


            frame.pack(); //和前面用的setPreferredSize搭配才有用
            frame.setVisible(true);
        }
    }
    //  实现ActionListener接口的方法
    public void actionPerformed(ActionEvent e){
        //导入文件
        if(e.getSource()==fMenuLoad){
            try {
                JFileChooser fd = new JFileChooser();
                fd.setFileSelectionMode(JFileChooser.OPEN_DIALOG);
                fd.showOpenDialog(null);
                File f = fd.getSelectedFile();
                if (f != null) {
                    String filepath = f.getPath().trim();
                    //读取本地txt
                    StringBuffer buffer = new StringBuffer();
                    BufferedReader bf= new BufferedReader(new FileReader(filepath));
                    String s = null;
                    while((s = bf.readLine())!=null){//使用readLine方法，一次读一行
                        buffer.append(s.trim());
                    }
                    String xml = buffer.toString();
                    PublicData.Text= xml;
                    if(isInit==false)
                    {
                        showFrame();
                        isInit=true;
                    }
                    else
                    {
                        this.edit.txtOriginal.setText(PublicData.Text);
                    }
                }
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        //退出按钮
        if(e.getSource()==fMenuExit){
            int i=JOptionPane.showConfirmDialog(this,"你真的要退出吗?","疯狂打字",
                    JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
            if(i==0){
                System.exit(0);
            }
        }
        //隐藏提示键盘
        if(e.getSource()==fMenuCoverkb){
            if(jianpan!=null)
            {
                this.jianpan.setVisible(false);
            }

        }
        //打开提示键盘
        if(e.getSource()==fMenuOpenkb){
            if(jianpan!=null) {
                this.jianpan.setVisible(true);
            }

        }
        //键盘练习按钮
        if(e.getSource()==fMenuKey){
            PublicData.Text=PublicData.getText((int)(Math.random()*5+1));
            if(isInit==false) {
                showFrame();
                isInit=true;
            } else {
                JOptionPane.showMessageDialog(new JPanel(),"请先点击刷新文本","提示",JOptionPane.WARNING_MESSAGE);
            }
        }
        // 刷新文本按钮
        if(e.getSource()==fMenuRefresh){
            if(isInit==false) {
                JOptionPane.showMessageDialog(new JPanel(),"请先点击键盘练习","提示",JOptionPane.WARNING_MESSAGE);
            } else {
                PublicData.Text=PublicData.getText((int)(Math.random()*6+1));
                this.edit.txtOriginal.setText(PublicData.Text);
                this.edit.txtNewText.setText("");
            }

        }
    }
    //显示游戏界面
    private void showFrame(){
        //显示键盘
        //keyset是jpanel，需要放到JInternalFrame上才能显示
        jianpan=new JInternalFrame();
        jianpan.setBounds(0,326,0,0);
        jianpan.setPreferredSize(new java.awt.Dimension(800, 223));
        //隐藏窗体的顶部title bar
        ((javax.swing.plaf.basic.BasicInternalFrameUI) jianpan.getUI()).setNorthPane(null);
        //putClientProperty用来为组件设置注释，后面的true意味取消掉窗体边框，不同的组件含义不同
        jianpan.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
        jianpan.setVisible(true);
        //搭配setPreferredSize使用，setPreferredSize设置为合适的大小，pack为之分配大小。如果不pack则大小为0
        jianpan.pack();
        Keyset ks=new Keyset();
//		keylistener kl=new keylistener();
//		ks.add(kl);
        jianpan.add(ks);
        this.add(jianpan,2);


//		//显示文本和输入文本窗体
        edit=new Editor(this);
        this.add(edit,2);
    }

    /**
     * 重置图片的尺寸
     * @param image
     * @param width
     * @param height
     * @return
     */
    public BufferedImage resetImagesSize(Image image,int width,int height){
        final BufferedImage bufferedImage = new BufferedImage(width,height , BufferedImage.TYPE_INT_RGB);
        final Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.setComposite(AlphaComposite.Src);
        //below three lines are for RenderingHints for better image quality at cost of higher processing time
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.drawImage(image, 0, 0, width,height , null);
        graphics2D.dispose();
        return bufferedImage;
    }
}